package com.foreknow.web;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.User;
import com.foreknow.service.UserService;
import com.foreknow.utils.Constance;
import com.foreknow.utils.TokenUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public String login(@RequestBody User user) throws JsonProcessingException {
        User userLogin = userService.LoginService(user.getUsername(), user.getPassword());
        HashMap<String, Object> hs = new HashMap<>();
        if (userLogin != null) {
            String token = TokenUtil.sign(userLogin);
            userLogin.setToken(token);
            hs.put("token", token);
            hs.put("data", userLogin);
            hs.put("meta", new Constance("登陆成功", 200));
        } else {
            hs.put("meta", new Constance("登陆失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/findUserByPage", method = RequestMethod.POST)
    @ResponseBody
    public String findUserByPage(@RequestBody Map<String, Object> param) throws JsonProcessingException {
        //设置分页信息
        int pagenum = (int) param.get("pagenum");
        int pagesize = (int) param.get("pagesize");
        PageHelper.startPage(pagenum, pagesize);
        User user = new User();
        user.setUsername((String) param.get("username"));
        user.setPassword((String) param.get("passwprd"));
        user.setRealname((String) param.get("realname"));
        if (!((String) param.get("usetype")).equals("")) {
            user.setUsetype(Integer.parseInt((String) param.get("usetype")));
        }
        if (!((String) param.get("doctitleid")).equals("")) {
            user.setDoctitleid(Integer.parseInt((String) param.get("doctitleid")));
        }
        user.setIsscheduling((String) param.get("isscheduling"));
        if (!((String) param.get("deptid")).equals("")) {
            user.setDeptid(Integer.parseInt((String) param.get("deptid")));
        }
        if (!((String) param.get("registleid")).equals("")) {
            user.setRegistleid(Integer.parseInt((String) param.get("registleid")));
        }
        List<User> userList = userService.getQueryAllUser(user);
        PageInfo info = new PageInfo(userList);
        HashMap<String, Object> hs = new HashMap<>();
        hs.put("data", info);
        if (userList.size() > 0) {
            hs.put("meta", new Constance("获取用户列表成功", 200));
        } else {
            hs.put("meta", new Constance("获取用户列表失败", 401));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/insertUser", method = RequestMethod.POST)
    @ResponseBody
    public String insertUserInfo(@RequestBody User user) throws JsonProcessingException {
        int x = userService.insertUser(user);
        HashMap<String, Object> hs = new HashMap<>();
        if (x == 1) {
            hs.put("meta", new Constance("添加用户成功", 200));
        } else {
            hs.put("meta", new Constance("添加用户失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/deleteUser", method = RequestMethod.GET)
    @ResponseBody
    public String deleteUserById(@RequestParam Integer userId) throws JsonProcessingException {
        int x = userService.deleteUser(userId);
        HashMap<String, Object> hs = new HashMap<>();
        if (x == 1) {
            hs.put("meta", new Constance("删除用户成功", 200));
        } else {
            hs.put("meta", new Constance("删除用户失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/selectUser", method = RequestMethod.GET)
    @ResponseBody
    public String selectUserById(@RequestParam Integer userId) throws JsonProcessingException {
        User user = userService.selectUser(userId);
        HashMap<String, Object> hs = new HashMap<>();
        if (user != null) {
            hs.put("data", user);
            hs.put("meta", new Constance("查询用户成功", 200));
        } else {
            hs.put("meta", new Constance("查询用户失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    @ResponseBody
    public String updateUserById(@RequestBody User user) throws JsonProcessingException {
        int x = userService.updateUser(user);
        HashMap<String, Object> hs = new HashMap<>();
        if (x == 1) {
            hs.put("meta", new Constance("修改用户成功", 200));
        } else {
            hs.put("meta", new Constance("修改用户失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/deleteList", method = RequestMethod.POST)
    @ResponseBody
    public String deleteList(@RequestBody() Object[] param) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        if (param != null) {
            for (int i = 0; i < param.length; i++) {
                userService.deleteUser((Integer) param[i]);
            }
            hs.put("meta", new Constance("批量删除成功", 200));
        } else {
            hs.put("meta", new Constance("批量删除失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/test", method = RequestMethod.POST)
    @ResponseBody
    public String test(@RequestBody Map<String, Object> para) throws JsonProcessingException {
        HashMap<String, Object> hs = new HashMap<>();
        hs.put("data", "data");
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
}